package com.sinodata.bsm.cicp.collector.sqlserver;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.constants.SeparatorConstants;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class Configure extends AbstractJDBCCollector {
    private static final Logger logger = Logger.getLogger(Configure.class);

    //    private static final int DEADLOCK_WAIT_TIME = 60;

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        //        String serverName = this.getParamMap().get("服务器名");
        String serverName = this.getParamMap().get("ServerName");
        System.out.println(serverName);

        String sql = "sp_configure";
        String[] rs;
        try {
            rs = this.getStrings(serverName, sql, ip);

            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            for (int i = 0; i < propMetas.length; i++) {
                propValues[i] = ResPropertyValue.createPropValue(rs[i], propMetas[i]);
            }

            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (CollectorException e) {
            logger.error("while get collector", e);
            throw new CollectorException(e);
        }
        return values;
    }

    private String[] getStrings(String serverName, String sql, String ip) throws CollectorException {
        String[] propValArr = new String[1];
        try {

            ResultSet rst = this.getCachedResultSet("sqlserver", ip, serverName, sql);
            if (rst != null) {
                StringBuffer configureInfo = new StringBuffer();
                // 名称,最小值,最大值,配置值,运行值
                //                configureInfo.append("Name").append(SeparatorConstants.COL_SEPARATOR);//名称
                //                configureInfo.append("Min Value").append(SeparatorConstants.COL_SEPARATOR);//最小值
                //                configureInfo.append("Max Value").append(SeparatorConstants.COL_SEPARATOR);//最大值
                //                configureInfo.append("Config Value").append(SeparatorConstants.COL_SEPARATOR);//配置值
                //                configureInfo.append("Runtime Value").append(SeparatorConstants.COL_SEPARATOR);//运行值
                //                configureInfo.append(SeparatorConstants.LIN_SEPARATOR);
                while (rst.next()) {
                    String line = getTabLineStr(rst, 5);
                    configureInfo.append(line).append(SeparatorConstants.LIN_SEPARATOR);
                }
                String configureInfostr = configureInfo.toString();

                propValArr[0] = configureInfostr;
            } else {
                return null;
            }

        } catch (Exception ex) {
            logger.error("while connect database", ex);
            throw new CollectorException(ex);
        }

        return propValArr;
    }

    private String getTabLineStr(ResultSet rst, int colNum) throws CollectorException {
        StringBuffer tabLineStr = new StringBuffer();
        try {
            for (int i = 1; i <= colNum; i++) {
                String val = rst.getString(i);
                if (val == null || val.trim().length() == 0) {
                    val = " ";
                } else
                    val = val.trim();
                tabLineStr.append(val).append(SeparatorConstants.COL_SEPARATOR);
            }
        } catch (Exception e) {
            logger.error("failed to get TabLineStr", e);
            throw new CollectorException(e);

        }
        return tabLineStr.toString();
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "ServerName" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "配置信息" };
        return new String[] { "ConfigureInfo" };
    }

}
